using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Resources;
using System.Windows.Forms;
using FlexCell;
using Infragistics.Win.UltraWinTabControl;
using PcrDataAnalysis;
using PluginFrame;


namespace PcrDataAnalysis
{
    public class AbsoluteAnalysisParameters : Form
    {
        protected ResourceManager mResourceManager;

        protected List<IPcrExperimentProject> mPcrExerimentProjects;

        protected UltraTabControl mOptionsTabControl;

        protected IConfigurationService mConfigurationService;

        protected IPcrSystemService mPcrSystemService;

        protected IPcrLogService mPcrLogService;

        protected IPcrExperiment mPcrExperiment;

        protected IAnalysisService mAnalysisService;

        private IContainer components;

        protected Panel panel1;

        protected Button btnConfirm;

        protected Button btnCancel;

        protected CheckBox chkAutoAnalysis;

        protected CheckBox chkShowParameterPanel;

        public AbsoluteAnalysisParameters(IApplication app, List<IPcrExperimentProject> pcrExerimentProjects, bool isShowAutoAnalysis)
        {
            InitializeComponent();
            mConfigurationService = (IConfigurationService)app.GetService("ConfigurationService");
            mAnalysisService = (IAnalysisService)app.GetService("AnalysisService");
            mPcrSystemService = (IPcrSystemService)app.GetService("PcrSystemService");
            mPcrLogService = (IPcrLogService)app.GetService("PcrLogService");
            mPcrExerimentProjects = pcrExerimentProjects;
            mResourceManager = ResourceManager.CreateFileBasedResourceManager("PcrDataAnalysis", app.GetResourceFolder() + GetType().Namespace + "\\", null);
            InitializeTab();
            btnConfirm.Text = mResourceManager.GetString("ConfirmString");
            btnCancel.Text = mResourceManager.GetString("CancelString");
            chkAutoAnalysis.Checked = isShowAutoAnalysis;
            chkAutoAnalysis.Visible = isShowAutoAnalysis;
            chkShowParameterPanel.Visible = true;
            base.DialogResult = DialogResult.Cancel;
        }

        protected void InitializeTab()
        {
            mPcrExperiment = mPcrSystemService.GetActiveFormCurrentExperiment(null);
            chkShowParameterPanel.Checked = !mPcrExperiment.IsHideParameterPanel;
            bool isLockGrid = false;
            if (mPcrExperiment.HasPassword && !mPcrExperiment.PcrStructure.PcrDataFile.IsNewPcrFile && !mPcrExperiment.IsPasswordMatch)
            {
                isLockGrid = true;
            }
            mOptionsTabControl = mAnalysisService.GenerateAnalysisOptionTabControl(mPcrExerimentProjects, true, isLockGrid);
            foreach (IPcrExperimentProject mPcrExerimentProject in mPcrExerimentProjects)
            {
                UltraTabControl ultraTabControl = mOptionsTabControl.Tabs[mPcrExerimentProject.Name].TabPage.Controls[0] as UltraTabControl;
                if (ultraTabControl != null)
                {
                    mAnalysisService.RefreshMeltingAnalysisOptionTab(ultraTabControl, mPcrExerimentProject);
                }
                if (mPcrExerimentProject.PcrProgram.AmplificationDetectionCount == 0)
                {
                    ultraTabControl.Tabs[0].Enabled = false;
                    if (ultraTabControl.Tabs.Count >= 1)
                    {
                        ultraTabControl.SelectedTab = ultraTabControl.Tabs[1];
                        ultraTabControl.ActiveTab = ultraTabControl.Tabs[1];
                    }
                }
            }
            base.Controls.Add(mOptionsTabControl);
            mOptionsTabControl.BringToFront();
            panel1.SendToBack();
        }

        protected void RefreshManualThresholdLocked(Grid grid, int rowIndex)
        {
            if (grid.Cell(rowIndex, 4).BooleanValue)
            {
                grid.Cell(rowIndex, 5).Locked = true;
                grid.Cell(rowIndex, 5).BackColor = Color.LightGray;
            }
            else
            {
                grid.Cell(rowIndex, 5).Locked = false;
                grid.Cell(rowIndex, 5).BackColor = Color.White;
            }
        }

        protected bool CheckEmptyOptionInGrid(Grid grid, int rowIndex)
        {
            for (int i = 1; i < grid.Cols; i++)
            {
                string text = grid.Cell(rowIndex, 1).Text;
                if (grid.Cell(rowIndex, i).Text.Length == 0)
                {
                    MessageBox.Show(string.Concat(((Control)(object)grid).Tag, " ", text, " \"", grid.Cell(0, i).Text, "\" ", mResourceManager.GetString("CanNotBeEmpty")), mResourceManager.GetString("ErrorString"));
                    return false;
                }
            }
            return true;
        }

        protected bool CheckBaseLineOptionInGrid(IPcrExperimentProject project, string channelName, Grid grid, int rowIndex, ref int startBaseLine, ref int stopBaseLine)
        {
            startBaseLine = grid.Cell(rowIndex, 2).IntegerValue;
            stopBaseLine = grid.Cell(rowIndex, 3).IntegerValue;
            if (startBaseLine <= 0)
            {
                MessageBox.Show(mResourceManager.GetString("ProjectString") + project.Name + ":" + mResourceManager.GetString("ChannelString") + channelName + " \"" + grid.Cell(0, 2).Text + "\" " + mResourceManager.GetString("BeLargerThanZero") + "!", mResourceManager.GetString("ErrorString"));
                return false;
            }
            if (startBaseLine >= stopBaseLine)
            {
                MessageBox.Show(mResourceManager.GetString("ProjectString") + project.Name + ":" + mResourceManager.GetString("ChannelString") + channelName + " \"" + grid.Cell(0, 2).Text + "\" " + mResourceManager.GetString("BeLessThan") + "\"" + grid.Cell(0, 3).Text + "\" !", mResourceManager.GetString("ErrorString"));
                return false;
            }
            if (grid.Cell(rowIndex, 6).Text.CompareTo(mResourceManager.GetString("AutoString")) == 0)
            {
                int num = (int)mConfigurationService.AnalysisConfiguration.AutoOptimizationMinStartValue;
                int num2 = (int)mConfigurationService.AnalysisConfiguration.AutoOptimizationMaxStopValue;
                if (startBaseLine < num)
                {
                    MessageBox.Show(mResourceManager.GetString("InAutoOptimization") + ", " + mResourceManager.GetString("ProjectString") + project.Name + ": " + mResourceManager.GetString("ChannelString") + channelName + " \"" + grid.Cell(0, 2).Text + "\" " + mResourceManager.GetString("MustBeLargerThan") + num + "!", mResourceManager.GetString("ErrorString"));
                    return false;
                }
                if (stopBaseLine > num2)
                {
                    MessageBox.Show(mResourceManager.GetString("InAutoOptimization") + ", " + mResourceManager.GetString("ProjectString") + project.Name + ":" + mResourceManager.GetString("ChannelString") + channelName + " \"" + grid.Cell(0, 3).Text + "\" " + mResourceManager.GetString("BeLessThan") + num2 + "!", mResourceManager.GetString("ErrorString"));
                    return false;
                }
            }
            else
            {
                try
                {
                    if (stopBaseLine > project.PcrProgram.AmplificationDetectionCount)
                    {
                        MessageBox.Show(string.Concat(mResourceManager.GetString("ProjectString"), ((Control)(object)grid).Tag, ":", mResourceManager.GetString("ChannelString"), channelName, " \"", grid.Cell(0, 3).Text, "\" ", mResourceManager.GetString("BeLessThanDetectionCycles")), mResourceManager.GetString("ErrorString"));
                        return false;
                    }
                }
                catch (Exception)
                {
                }
            }
            return true;
        }

        protected bool CheckManualThresholdInGrid(string channelName, Grid grid, int rowIndex, ref double manualThreshold)
        {
            manualThreshold = grid.Cell(rowIndex, 5).DoubleValue;
            if (manualThreshold <= 0.0)
            {
                MessageBox.Show(string.Concat(((Control)(object)grid).Tag, " ", channelName, " \"", grid.Cell(0, 5).Text, "\" ", mResourceManager.GetString("BeLargerThanZero")), mResourceManager.GetString("ErrorString"));
                return false;
            }
            return true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Cancel;
            Close();
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                mPcrExperiment.IsHideParameterPanel = !chkShowParameterPanel.Checked;
                if (!mPcrExperiment.PcrStructure.PcrDataFile.IsNewPcrFile)
                {
                    mPcrSystemService.SetActiveFormRightPanelVisible(chkShowParameterPanel.Checked);
                }
                string value = mAnalysisService.SaveAnalysisOptionTabControl(mOptionsTabControl);
                if (!string.IsNullOrEmpty(value))
                {
                    MessageBox.Show(value, mResourceManager.GetString("ErrorString"));
                    return;
                }
                IPcrExperiment activeFormCurrentExperiment = mPcrSystemService.GetActiveFormCurrentExperiment(null);
                if (!activeFormCurrentExperiment.PcrStructure.PcrDataFile.IsNewPcrFile)
                {
                    mPcrSystemService.AnalysisActiveForm();
                }
                base.DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                mPcrLogService.ShowExceptionDialogBox(ex);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PcrDataAnalysis.AbsoluteAnalysisParameters));
            this.panel1 = new System.Windows.Forms.Panel();
            this.chkShowParameterPanel = new System.Windows.Forms.CheckBox();
            this.chkAutoAnalysis = new System.Windows.Forms.CheckBox();
            this.btnConfirm = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            base.SuspendLayout();
            this.panel1.Controls.Add(this.chkShowParameterPanel);
            this.panel1.Controls.Add(this.chkAutoAnalysis);
            this.panel1.Controls.Add(this.btnConfirm);
            this.panel1.Controls.Add(this.btnCancel);
            resources.ApplyResources(this.panel1, "panel1");
            this.panel1.Name = "panel1";
            resources.ApplyResources(this.chkShowParameterPanel, "chkShowParameterPanel");
            this.chkShowParameterPanel.Name = "chkShowParameterPanel";
            this.chkShowParameterPanel.UseVisualStyleBackColor = true;
            resources.ApplyResources(this.chkAutoAnalysis, "chkAutoAnalysis");
            this.chkAutoAnalysis.Name = "chkAutoAnalysis";
            this.chkAutoAnalysis.UseVisualStyleBackColor = true;
            resources.ApplyResources(this.btnConfirm, "btnConfirm");
            this.btnConfirm.Name = "btnConfirm";
            this.btnConfirm.UseVisualStyleBackColor = true;
            this.btnConfirm.Click += new System.EventHandler(btnConfirm_Click);
            resources.ApplyResources(this.btnCancel, "btnCancel");
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
            resources.ApplyResources(this, "$this");
            base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            base.Controls.Add(this.panel1);
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "AbsoluteAnalysisParameters";
            base.ShowIcon = false;
            base.ShowInTaskbar = false;
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            base.ResumeLayout(false);
        }
    }
}
